import axios from 'axios'
import api from '@/utils/request'
import { cubicSplineInterpolation } from '@/api/math'

class CapacityStorage {
    capacityStorageList = [
        { device: '2023072701', name: '大田坑水库库容曲线', file: '大田坑水库库容曲线.json' },
        { device: '2023072701', name: '大西坑水库库容曲线-黄基', file: '大西坑水库库容曲线-黄基.json' },
        { device: '2023072701', name: '大西坑水库库容曲线-珠基', file: '大西坑水库库容曲线-珠基.json' },
        { device: '2023072701', name: '公坑水库库容曲线', file: '公坑水库库容曲线.json' },
        { device: '2023072701', name: '观音堂水库库容曲线', file: '观音堂水库库容曲线.json' },
        { device: '2023072701', name: '鲤鱼山水库库容曲线', file: '鲤鱼山水库库容曲线.json' },
        { device: '2023072701', name: '六联水库库容曲线', file: '六联水库库容曲线.json' },
        { device: '2023072701', name: '龙岗坑水库库容曲线', file: '龙岗坑水库库容曲线.json' },
        { device: '2023072701', name: '棋杆石水库库容曲线', file: '棋杆石水库库容曲线.json' },
        { device: '2023072701', name: '三堡水库库容曲线', file: '三堡水库库容曲线.json' },
        { device: '2023072701', name: '小迳水库库容曲线', file: '小迳水库库容曲线.json' },
        { device: '2023072701', name: '那围水库库容曲线', file: '那围水库库容曲线.json' },
        { device: '2023072701', name: '凤飞云水库库容曲线', file: '凤飞云水库库容曲线.json' },
        { device: '2023072701', name: '兰石水库库容曲线', file: '兰石水库库容曲线.json' },
        { device: '2023072701', name: '钳口水库库容曲线', file: '钳口水库库容曲线.json' },
        { device: '2023072701', name: '那咀水库库容曲线', file: '那咀水库库容曲线.json' }
    ]

    constructor(name) {
        this.changeLocation(name)
    }

    getDeviceNameList() {
        return this.capacityStorageList.map(({ device, name }) => ({ id: device, label: name }))
    }

    async getWaterStorageData() {
        const fileName = this.capacityStorageList.find(({ name }) => name === this.name)?.file
        return fileName ? (await axios.get(`/data/库容曲线/${fileName}`)).data : []
    }

    changeLocation(name) {
        this.name = name
    }

    async getCurrentWaterLevel() {
        const { device } = this.capacityStorageList.find(({ name }) => name === this.name) || {}
        const response = await api.get(`/getSiteByNewWaterLevelData?device=${device}`)
        const waterLevelData = parseFloat(response.data.data)
        const waterLevel = isNaN(waterLevelData) ? '0' : waterLevelData.toFixed(2)
        const waterStorageData = await this.getWaterStorageData()
        const capacityStorage = cubicSplineInterpolation(
            waterLevel,
            waterStorageData.map(({ height }) => height),
            waterStorageData.map(({ value }) => value)
        )
        return capacityStorage ? { waterLevel, capacityStorage } : null
    }
}

const capacityStorageBaseOption = {
    tooltip: {
        trigger: 'axis',
        textStyle: {
            align: 'left'
        }
    },
    grid: {
        top: 40,
        bottom: 25,
        left: 55,
        right: 20
    },
    toolbox: {
        show: true
    },
    calculable: true,
    xAxis: [
        {
            type: 'value',
            axisLine: {
                lineStyle: {
                    color: '#FFF'
                }
            }
        }
    ],
    yAxis: [
        {
            type: 'value',
            name: '水位(m)',
            min: 0,
            axisLine: {
                lineStyle: {
                    color: '#FFF'
                }
            },
            axisLabel: {}
        }
    ],
    series: [
        {
            type: 'line',
            name: '库容曲线',
            data: [
                [1.5, 22.6],
                [2.4, 23.5],
                [4, 24.5],
                [7, 25.5],
                [10.7, 26.5],
                [15.1, 27.5],
                [20.1, 28.5],
                [26, 29.5],
                [32.9, 30.5],
                [41.3, 31.5],
                [52.1, 32.5],
                [63.1, 33.5],
                [76.9, 34.5],
                [91.8, 35.5],
                [107.4, 36.5],
                [123.98, 37.5],
                [142.82, 38.5],
                [162.74, 39.5],
                [177.79, 40],
                [187, 40.76]
            ],
            smooth: true,
            showSymbol: false,
            itemStyle: {
                color: '#3498db'
            },
            lineStyle: {
                normal: {
                    type: 'solid'
                }
            },
            markLine: {
                label: {
                    normal: {
                        formatter: ''
                    }
                },
                lineStyle: {
                    normal: {
                        color: '#EE0C0C',
                        width: 2
                    }
                },
                data: [
                    [
                        {
                            symbol: 'none',
                            xAxis: '0',
                            yAxis: 35.31
                        },
                        {
                            xAxis: '88.97',
                            yAxis: 35.31
                        }
                    ],
                    [
                        {
                            symbol: 'none',
                            xAxis: '88.97',
                            yAxis: 0
                        },
                        {
                            symbol: 'none',
                            xAxis: '88.97',
                            yAxis: 35.31
                        }
                    ]
                ]
            }
        }
    ]
}

export { CapacityStorage, capacityStorageBaseOption }
